package com.rocker.graduation.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.rocker.graduation.dao.CategoryMapper;
import com.rocker.graduation.dao.EntityMapper;
import com.rocker.graduation.dao.InventoryMapper;
import com.rocker.graduation.domain.Category;
import com.rocker.graduation.domain.CategoryExample;
import com.rocker.graduation.entity.CategoryBean;
import com.rocker.graduation.service.ICategoryService;

@Service
public class CategoryService implements ICategoryService {
	@Autowired
	private CategoryMapper categoryMapper;
	
	@Autowired
	private InventoryMapper inventoryMapper;
	
	@Autowired
	private EntityMapper entityMapper;
	
	/* (non-Javadoc)
	 * @see com.rocker.graduation.service.impl.ICategoryService#getCategoriesNum(java.lang.String, java.lang.String)
	 */
	@Override
	public int getCategoriesNum(String sort, String type){
		CategoryExample example = new CategoryExample();
		if (type.equals("4")) {
			example.createCriteria().andCategoryCityEqualTo(sort);
		}else if (type.equals("3")) {
			example.createCriteria().andCategoryProvinceEqualTo(sort);
		}else if (type.equals("2")) {
			example.createCriteria().andCategoryAreaEqualTo(sort);
		}else if (type.equals("1")) {
			example.createCriteria().andCategoryCountryEqualTo(sort);
		}
		return categoryMapper.countByExample(example);
	}
	
	/* (non-Javadoc)
	 * @see com.rocker.graduation.service.impl.ICategoryService#listCategoriesByPage(java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public List<CategoryBean> listCategoriesByPage(String sort, String type, String page){
		List<CategoryBean> categoryBeans = new ArrayList<CategoryBean>();
		CategoryExample example = new CategoryExample();
		example.setLimit(page);
		if (type.equals("4")) {
			example.createCriteria().andCategoryCityEqualTo(sort);
		}else if (type.equals("3")) {
			example.createCriteria().andCategoryProvinceEqualTo(sort);
		}else if (type.equals("2")) {
			example.createCriteria().andCategoryAreaEqualTo(sort);
		}else if (type.equals("1")) {
			example.createCriteria().andCategoryCountryEqualTo(sort);
		}
		
		List<Category> categories = categoryMapper.selectByExample(example);
		
		for (Category category : categories) {
			CategoryBean categoryBean = new CategoryBean();
			List<Integer> imageId = entityMapper.getImageId(category.getCategoryId());
			categoryBean.setPrice(category.getCategoryPrice());
			categoryBean.setItemId(category.getCategoryId());
			categoryBean.setTitle(category.getCategoryName());
			categoryBean.setLongtitude(category.getCategoryLongtitude());
			categoryBean.setLatitude(category.getCategoryLatitude());
			categoryBean.setResolution(category.getCategoryResolution());
			categoryBean.setOrigin(category.getCategoryOrigin());
			categoryBean.setProportion(category.getCategoryProportion());
			categoryBean.setImageId(imageId);
			categoryBeans.add(categoryBean);
		}
		
		return categoryBeans;
	}
	
	/* (non-Javadoc)
	 * @see com.rocker.graduation.service.impl.ICategoryService#getCategoryInfo(java.lang.Integer)
	 */
	@Override
	public CategoryBean getCategoryInfo(Integer cateId){
		CategoryBean categoryBean = new CategoryBean();
		
		Category category = categoryMapper.selectByPrimaryKey(cateId);
		List<Integer> imageId = entityMapper.getImageId(cateId);
		categoryBean.setPrice(category.getCategoryPrice());
		categoryBean.setItemId(category.getCategoryId());
		categoryBean.setTitle(category.getCategoryName());
		categoryBean.setLongtitude(category.getCategoryLongtitude());
		categoryBean.setLatitude(category.getCategoryLatitude());
		categoryBean.setResolution(category.getCategoryResolution());
		categoryBean.setOrigin(category.getCategoryOrigin());
		categoryBean.setProportion(category.getCategoryProportion());
		categoryBean.setImageId(imageId);
		return categoryBean;
	}
	
	/* (non-Javadoc)
	 * @see com.rocker.graduation.service.impl.ICategoryService#getCategorySales()
	 */
	@Override
	public List<CategoryBean> getCategorySales(){
		List<Category> categories = categoryMapper.getCategorySales();
		List<CategoryBean> categoryBeans = new ArrayList<CategoryBean>();
		for (Category category : categories) {
			List<Integer> imageIdList = entityMapper.getImageId(category.getCategoryId());
			CategoryBean categoryBean = new CategoryBean();
			categoryBean.setItemId(category.getCategoryId());
			categoryBean.setImageId(imageIdList);
			categoryBean.setPrice(category.getCategoryPrice());
			categoryBean.setTitle(category.getCategoryName());
			categoryBeans.add(categoryBean);
		}
		
		return categoryBeans;
	}
	
	/* (non-Javadoc)
	 * @see com.rocker.graduation.service.impl.ICategoryService#getSearchItem(java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public List<CategoryBean> getSearchItem(String sort, String page, String type){
		List<CategoryBean> categoryBeans = new ArrayList<CategoryBean>();
		CategoryExample example = new CategoryExample();
		example.setLimit(page);
		if (type.equals("4")) {
			example.createCriteria().andCategoryCityEqualTo(sort);
		}else if (type.equals("3")) {
			example.createCriteria().andCategoryProvinceEqualTo(sort);
		}
		
		List<Category> categories = categoryMapper.selectByExample(example);
		
		for (Category category : categories) {
			CategoryBean categoryBean = new CategoryBean();
			List<Integer> imageId = entityMapper.getImageId(category.getCategoryId());
			categoryBean.setPrice(category.getCategoryPrice());
			categoryBean.setItemId(category.getCategoryId());
			categoryBean.setTitle(category.getCategoryName());
			categoryBean.setLongtitude(category.getCategoryLongtitude());
			categoryBean.setLatitude(category.getCategoryLatitude());
			categoryBean.setResolution(category.getCategoryResolution());
			categoryBean.setOrigin(category.getCategoryOrigin());
			categoryBean.setProportion(category.getCategoryProportion());
			categoryBean.setImageId(imageId);
			categoryBeans.add(categoryBean);
		}
		return categoryBeans;
	}
}
